package org.springframework.session.config.annotation.web.server;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.ReactiveSessionRepository;
import org.springframework.session.Session;
import org.springframework.session.web.server.session.SpringSessionWebSessionStore;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
import org.springframework.web.server.session.DefaultWebSessionManager;
import org.springframework.web.server.session.WebSessionIdResolver;
import org.springframework.web.server.session.WebSessionManager;

@Configuration(proxyBeanMethods = false)
public class SpringWebSessionConfiguration {

	private WebSessionIdResolver webSessionIdResolver;

	@Autowired(required = false)
	public void setWebSessionIdResolver(WebSessionIdResolver webSessionIdResolver) {
		this.webSessionIdResolver = webSessionIdResolver;
	}

	@Bean(WebHttpHandlerBuilder.WEB_SESSION_MANAGER_BEAN_NAME)
	public WebSessionManager webSessionManager(ReactiveSessionRepository<? extends Session> repository) {
		SpringSessionWebSessionStore<? extends Session> sessionStore = new SpringSessionWebSessionStore<>(repository);
		DefaultWebSessionManager manager = new DefaultWebSessionManager();
		manager.setSessionStore(sessionStore);
		if (this.webSessionIdResolver != null) {
			manager.setSessionIdResolver(this.webSessionIdResolver);
		}
		return manager;
	}
}
